package com.rongchuang.controller;

import com.rongchuang.dto.RoleDTO;
import com.rongchuang.dto.UserDTO;
import com.rongchuang.service.RolePermissionService;
import com.rongchuang.service.UserRoleService;
import com.rongchuang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 分页查询用户信息
     */
    @GetMapping("page")
    public Page<UserDTO> pageSearch(@RequestParam(defaultValue = "0") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize,
                                    String username) {
        return userService.pageSearch(pageNum, pageSize, username);
    }

    /**
     * 保存用户信息
     */
    @PostMapping("modify")
    public void modify(@RequestBody UserDTO dto) {
        userService.modify(dto);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("detail/{id}")
    public UserDTO detail(@PathVariable Long id) {
        return userService.getDetail(id);
    }

    /**
     * 更新状态
     */
    @PostMapping("update-status")
    public void updateStatus(Long id, Byte status) {
        userService.updateStatus(id, status);
    }

    /**
     * 获取用户关联角色
     * @param userId 用户id
     * @return 关联角色列表
     */
    @PostMapping("find-user-role")
    public List<RoleDTO> findUserRole(Long userId) {
        return userRoleService.findRoleIdByUserId(userId);
    }

    /**
     * 绑定角色
     */
    @PostMapping("binging-role")
    public void bindingRole(@RequestBody UserDTO userDTO) {
        userRoleService.addRole(userDTO.getId(), userDTO.getRoleIds());
    }

    /**
     * 角色分配权限
     * @param roleId 角色id
     * @param permissionIds 权限ids
     */
    @PostMapping("assign-permission")
    public void assignPermission(Long roleId, List<Long> permissionIds) {
        rolePermissionService.addPermission(roleId, permissionIds);
    }
}
